/*
This version features two improvements over the original serial version.  
1) Unnecessary iterations were avoided using a supplementary variable named "stay"
to exit loops when there were more iterations left but the alex number has been found. 
2) Unneeded temporary variables were removed and replaced with the values they held. Saving
the results caused a performance penalty as most of the iterations did not later use it. 
*/

#include <stdio.h>
#include <math.h>
#include <omp.h>

int main(int argc, char *argv[]) {

    int i,j,k,l,aflag,limit,prod;
    double temp;

    if (argc<2) 
        return 0;

    limit = atoi(argv[1]);

	int stay = 1;	// 1) exits loops prematurely to avoid fruitless iterations

    for (i=1;i<=limit;i++) 
    {
		stay = 1; 
        aflag=0;
        for (j=i;j>=-i && stay; j--) 
        {
            if (j==0) continue;
            for (k=i/j;k>=-i/j && stay; k--) 
            {
		        if (k==0) continue;
                for (l=i/(j*k);l<=-i/(j*k) && stay; l++) 
                {
			        if (j*k*l == i)		// 2) Unnecessary temporary variable removed 
                    {
						// 2) Unnecessary temp variable removed
                        if (fabs((1/(double)j)+(1/(double)k)+(1/(double)l)-(1/(double)i))<1e-9)
						{
							aflag=1;
                        	stay = 0;	// 1) This skips unnecessary iterations
						}
			        }
                }
            }
        }

        if (aflag) printf("%d\n",i);
    }
}


